package com.exam.controller;

import com.exam.pojo.entity.Student;
import com.exam.pojo.mode.DepartmentVo;
import com.exam.pojo.mode.StudentVo;
import com.exam.pojo.params.PageParam;
import com.exam.service.DepartmentService;
import com.exam.service.StudentService;
import com.exam.utils.PageUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class StudentController {
    @Resource
    private StudentService studentService;

    @Resource
    private DepartmentService departmentService;

    /**
     * 人员管理
     * @return
     */
//    @RequestMapping("user1.html")
//    public String findAll(Model model){
//        List<StudentVo> studentVoList = studentService.findAll();
//        List<DepartmentVo> departmentVoList = departmentService.findAllList();
//        model.addAttribute("departmentVoList",departmentVoList);
//
//        //分页
//        PageParam pageParam = new PageParam(studentVoList.size(),1,10);
//        model.addAttribute("pageParam",pageParam);
//
//        model.addAttribute("studentVoList",studentVoList);
//        return "user";
//    }

    /**
     * 首页  人员列表
     *
     * @param currentPage
     * @return
     */
    @RequestMapping("user.html")
    public String listStudent(Integer currentPage, Model model) {
        //统计总数
        List<StudentVo> studentVoList = studentService.findAll();
        int total = studentVoList.size();
        currentPage = PageUtils.getCurrentPage(currentPage,total);

        //根据当前页查询当前页的数据
        List<StudentVo> listStudentVo = studentService.listStudent((currentPage - 1) * 5, 5);
        model.addAttribute("studentVoList",listStudentVo);

        //部门岗位菜单
        List<DepartmentVo> departmentVoList = departmentService.findAllList();
        model.addAttribute("departmentVoList",departmentVoList);

        //将当前页、总数、每页数据个数（5：后期可以自定义）给前端
        PageParam pageParam = new PageParam(total,currentPage,5);
        model.addAttribute("pageParam",pageParam);
        return "user";
    }

    /**
     * 搜索
     * @return
     */
    @PostMapping("findStudentByNameOrdepartmentId")
    public String findStudentByNameOrdepartmentId(SearchStudentParam searchStudentVo, Model model){
        List<StudentVo> studentVoList = studentService.findStudentByDidOrPidOrName(searchStudentVo);
        int total = studentVoList.size();

        List<DepartmentVo> departmentVoList = departmentService.findAllList();
        model.addAttribute("departmentVoList",departmentVoList);
        model.addAttribute("studentVoList",studentVoList);
        PageParam pageParam = new PageParam(total,1,5);
        model.addAttribute("pageParam",pageParam);
        return "user";
    }

    /**
     * 添加人员
     * @param student
     */
    @PostMapping("addStudent")
    @ResponseBody
    public void addStudent(@RequestBody Student student){
        studentService.addStudent(student);
    }

    /**
     * 删除人员
     * @param studentId
     * @return
     */
    @PostMapping("delStudent/{studentId}")
    @ResponseBody
    public void delStudent(@PathVariable("studentId") Integer studentId){
        studentService.delStudent(studentId);
//        return "user";
    }

    /**
     * 批量删除人员
     * @param studentIdList
     * @return
     */
    @PostMapping("delAllStudent")
    public String delAllStudent(@RequestBody List<Integer> studentIdList){
        studentService.delAllStudent(studentIdList);
        return "user";
    }



    /**
     * 跳转编辑页
     * @param studentId
     * @param model
     * @return
     */
    @RequestMapping("toEditPage/{studentId}")
    public String toEditPage(@PathVariable("studentId") Integer studentId,Model model){
        List<DepartmentVo> departmentVoList = departmentService.findAllList();
        model.addAttribute("departmentVoList",departmentVoList);
        model.addAttribute("studentId",studentId);
        return "user-edit";
    }

    /**
     * 更新人员信息
     * @param student
     */
    @PostMapping("editStudent")
    @ResponseBody
    public void editStudent(@RequestBody Student student){
        studentService.editStudent(student);
    }




//    /**
//     * 人员管理
//     * @return
//     */
//    @RequestMapping("user.html/{page}")
//    public String findAll(@PathVariable("page") String page, Model model){
//        System.out.println(page);
//        Integer iPage = Integer.parseInt(page);
//        List<StudentVo> studentVoList = studentService.findAll(iPage,10);
//        System.out.println(studentVoList);
//        model.addAttribute("studentVoList",studentVoList);
//        return "user";
//    }

}
